package net.lb.io.json;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.FixMethodOrder;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import static org.junit.Assert.*;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class Speed
{
    int            count  = 10000;
    private Logger logger = LogManager.getLogger();
    
    @Test
    public void test_1_objectToStr()
    {
        Data person = new Data();
        long t0 = System.currentTimeMillis();
        for (int i = 0; i < count; i++)
        {
            JsonUtil.toJsonString(person);
        }
        long costLbjson = System.currentTimeMillis() - t0;
        logger.info("lbjson对象到字符串转换耗时:{}", costLbjson);
        t0 = System.currentTimeMillis();
        for (int i = 0; i < count; i++)
        {
            JSON.toJSONString(person);
        }
        long costFastJson = System.currentTimeMillis() - t0;
        logger.info("fastjson对象到字符串转换耗时：{}", costFastJson);
        assertTrue(costLbjson < costFastJson);
    }
    
    @Ignore
    @Test
    /**
     * 没必要比，fastjson的速度特别慢
     */
    public void test_2_objectToJson()
    {
        Data data = new Data();
        long t0 = System.currentTimeMillis();
        for (int i = 0; i < count; i++)
        {
            JsonUtil.fromObject(data);
        }
        long costLbjson = System.currentTimeMillis() - t0;
        logger.info("lbjson对象到json对象转换耗时:{}", costLbjson);
        t0 = System.currentTimeMillis();
        for (int i = 0; i < count; i++)
        {
            JSONObject.toJSON(data);
        }
        long costFastJson = System.currentTimeMillis() - t0;
        logger.info("fastjson对象到json对象转换耗时：{}", costFastJson);
        assertTrue(costLbjson < costFastJson);
    }
    
    // @Test(timeout=1000000)
    public void test_3_strToJson()
    {
        Data data = new Data();
        String str = JsonUtil.toJsonString(data);
        long t0 = System.currentTimeMillis();
        for (int i = 0; i < count; i++)
        {
            JsonUtil.fromString(str);
        }
        long costLbjson = System.currentTimeMillis() - t0;
        logger.info("lbjson字符串到json对象耗时:{}", costLbjson);
        t0 = System.currentTimeMillis();
        for (int i = 0; i < count; i++)
        {
            JSON.parse(str);
        }
        long costFastJson = System.currentTimeMillis() - t0;
        logger.info("fastjson字符串打牌json对象耗时：{}", costFastJson);
        assertTrue(costLbjson < costFastJson);
    }
}
